package cn.bearspark.api.service.main.impl;

import cn.bearspark.api.service.main.CategoryService;
import cn.bearspark.api.vo.main.category.AddCategoryVO;
import cn.bearspark.api.vo.main.category.CategorySimpleVO;
import cn.bearspark.api.vo.main.category.CategoryVO;
import cn.bearspark.api.vo.main.category.ModifyCategoryVO;
import cn.bearspark.article.dto.category.AddCategoryDTO;
import cn.bearspark.article.dto.category.ModifyCategoryDTO;
import cn.bearspark.article.rpc.CategoryRpc;
import cn.bearspark.common.constant.GlobalConstant;
import cn.bearspark.common.util.ConvertBeanUtil;
import cn.bearspark.framework.web.starter.context.BSRequestContext;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author f
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @DubboReference(check = GlobalConstant.DUBBO_CHECK)
    private CategoryRpc categoryRpc;

    @Override
    public void add(AddCategoryVO addCategoryVO) {
        long userId = BSRequestContext.getUserId();

        AddCategoryDTO addCategoryDTO = ConvertBeanUtil.convert(addCategoryVO, AddCategoryDTO.class);
        addCategoryDTO.setUserId(userId);
        categoryRpc.add(addCategoryDTO);
    }

    @Override
    public CategoryVO get(Long categoryId) {
        long userId = BSRequestContext.getUserId();

        return ConvertBeanUtil.convert(categoryRpc.get(userId, categoryId), CategoryVO.class);
    }

    @Override
    public void modify(ModifyCategoryVO modifyCategoryVO) {
        long userId = BSRequestContext.getUserId();

        ModifyCategoryDTO modifyCategoryDTO = ConvertBeanUtil.convert(modifyCategoryVO, ModifyCategoryDTO.class);
        modifyCategoryDTO.setUserId(userId);
        categoryRpc.modify(modifyCategoryDTO);
    }

    @Override
    public void delete(Long categoryId) {
        long userId = BSRequestContext.getUserId();

        categoryRpc.delete(userId, categoryId);
    }

    @Override
    public List<CategorySimpleVO> list(Long listedUserId) {
        long userId = BSRequestContext.getUserId();

        return ConvertBeanUtil.convertList(categoryRpc.list(userId, listedUserId), CategorySimpleVO.class);
    }
}
